package tech.yqyl.halo;

import cn.hutool.core.text.StrFormatter;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.google.common.base.Splitter;
import lombok.Data;
import tech.yqyl.StringUtil;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @className: TagStore
 * @description:
 * @author: caoyangjie
 * @date: 2024/9/1
 **/
@Data
public class TagStore extends HaloStore {
    private Map<String, TagEntity> cache = new HashMap<>();
    private final String post = HaloMarkdownBoot.server.getDomain().concat("/apis/content.halo.run/v1alpha1/tags");
    private final String list = HaloMarkdownBoot.server.getDomain().concat("/apis/api.console.halo.run/v1alpha1/tags?page={}&size={}&keyword={}");
    private final String delete = HaloMarkdownBoot.server.getDomain().concat("/apis/content.halo.run/v1alpha1/tags/{}");

    private String postTemplate = "{\n" +
            "  \"spec\": {\n" +
            "    \"displayName\": \"{}\",\n" +
            "    \"slug\": \"{}\",\n" +
            "    \"color\": \"#ffffff\",\n" +
            "    \"cover\": \"\"\n" +
            "  },\n" +
            "  \"apiVersion\": \"content.halo.run/v1alpha1\",\n" +
            "  \"kind\": \"Tag\",\n" +
            "  \"metadata\": {\n" +
            "    \"name\": \"\",\n" +
            "    \"generateName\": \"tag-\",\n" +
            "    \"annotations\": {}\n" +
            "  }\n" +
            "}";

    public String create(String tagName) {
        if (cache.isEmpty()) {
            list(0, 0, "");
        }
        if (cache.containsKey(tagName)) {
            return cache.get(tagName).getMetadataName();
        }
        String reqData = StrFormatter.format(postTemplate, tagName, StringUtil.hashString(tagName));
        String metadataName = post(post, HaloMarkdownBoot.server, () -> reqData, rspData -> {
            return JSONObject.parseObject(rspData).getJSONObject("metadata").getString("name");
        });
        list(0, 0, "");
        return metadataName;
    }

    public String displayName2MetadataName(String displayNames) {
        if (!StringUtil.isEmpty(displayNames)) {
            List<String> displayNameStr = Splitter.on(",").trimResults().omitEmptyStrings().splitToList(displayNames);
            return displayNameStr.stream().map(dn -> cache.get(dn).getMetadataName()).collect(Collectors.joining(","));
        }
        return displayNames;
    }

    public void delete(String tagName) {
        if( cache!=null && cache.isEmpty() ) {
            list(0,0,tagName);
        }
        if( cache.containsKey(tagName) ) {
            TagEntity tag = cache.get(tagName);
            String deleteUri = StrFormatter.format(delete, tag.getMetadataName());
            delete(deleteUri, HaloMarkdownBoot.server, ()->JSONObject.toJSONString(tag.getAkd()), rspData->{
                return rspData;
            });
        }
    }

    public Map<String, TagEntity> list(int size, int page, String search) {
        cache = list(StrFormatter.format(list, page <= 0 ? 1 : page, size <= 0 ? 5000 : size, StringUtil.isEmpty(search) ? "" : search), HaloMarkdownBoot.server, rspData -> {
            Map<String, TagEntity> tags = new HashMap<>();
            JSONArray items = JSONObject.parseObject(rspData).getJSONArray("items");
            items.stream().forEach(itemO -> {
                JSONObject item = (JSONObject) itemO;
                TagEntity tag = new TagEntity();
                tag.setTagName(item.getJSONObject("spec").getString("displayName"));
                tag.setSlugId(item.getJSONObject("spec").getString("slug"));
                tag.setMetadataName(item.getJSONObject("metadata").getString("name"));
                tag.setAkd(item);
                tags.put(tag.getTagName(), tag);
            });
            return tags;
        });
        return cache;
    }

    public static void main(String[] args) {
        TagStore store = new TagStore();
        store.list(0, 0, "");
        store.getCache().forEach((k, v) -> {
            System.out.println(StrFormatter.format("{}->{}", k, JSONObject.toJSON(v)));
        });
        initTags();
        store.getCache().forEach((k, v) -> {
            System.out.println(StrFormatter.format("{}->{}", k, JSONObject.toJSON(v)));
        });
    }

    private static void initTags() {
        TagStore store = new TagStore();

        String[] tags = new String[]{
//                "精通MySQL系列",
//                "精通互联网工程系列",
//                "精通渗透系列",
//                "精通运维系列",
//                "精通Spring注解系列",
//                "精通移动端系列",
//                "精通分布式数据库系列",
//                "精通高并发系列",
//                "精通Nginx系列",
//                "精通分布式缓存系列",
//                "精通搜索引擎系列",
//                "精通J2EE系列",
//                "精通分布式系列",
//                "精通设计模式系列",
//                "精通Spring全家桶系列",
//                "精通高可用系列",
//                "精通云原生系列",
                "各类问题修复",
                "开发类问题解决",
                "生产类问题解决",
                "语言学习系列",
                "如何学习英语",
                "高可扩展性"
        };
        for (String tag : tags) {
            store.create(tag);
        }
    }
}
